package com.sk.ccflower.server.common.procManager;

import java.io.File;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.sk.ccflower.lib.commonUtils.FileUtil;
import com.sk.ccflower.lib.commonUtils.StringUtil;
import com.sk.ccflower.server.lib.sysBeans.ProcInfoBean;
import com.sk.ccflower.server.lib.sysUtils.Environment;
import com.sk.ccflower.server.lib.sysUtils.EventHandler;
import com.sk.ccflower.server.lib.sysUtils.ProcLauncherEx;

/**
 * @author eunwoogi
 * @since 2009. 11. 11.
 */
public class ProcWorkEx {

	private FileUtil fUtil;
	private Logger log;

	public ProcWorkEx() {
		this.log = Logger.getLogger(getClass());
		this.fUtil = new FileUtil();
	}
	
	public ArrayList<String> loadProcessList() throws Exception {
		String path = Environment.getCfgProcDir() + File.separator
				+ "ProcWorkList.properties";
		String tStr = this.fUtil.readFile(path);

		String[] procNames = tStr.split("\n");
		ArrayList<String> procList = new ArrayList<String>();
		for (int i = 0; i < procNames.length; i++) {
			if (procNames[i].startsWith("#")
					|| (procNames[i].trim().length() == 0))
				continue;
			procList.add(procNames[i].trim());
		}
		return procList;
	}
	
	public ArrayList<ProcInfoBean> loadProcessListEx() throws Exception {
		String path = Environment.getCfgProcDir() + File.separator
				+ "ProcWorkListEx.properties";
		String tStr = this.fUtil.readFile(path);

		String[] lineStr = tStr.split("\n");
		ArrayList<ProcInfoBean> procList = new ArrayList<ProcInfoBean>();
		for (int i = 0; i < lineStr.length; i++) {
			if (lineStr[i].startsWith("#") || (lineStr[i].trim().length() == 0))
				continue;
			
			lineStr[i] = lineStr[i].replaceAll( "\r", "" );
			
			ProcInfoBean bean = new ProcInfoBean();
			
			if ( lineStr[i].indexOf(":") < 0 ) {
				bean.name = lineStr[i];
			} else {
				String[] values = StringUtil.toStringArray( lineStr[i], ":" );
				
				bean.name = values[0];
				bean.cflag = new Short( values[1] ).shortValue();
			}
			
			procList.add( bean );
		}
		
		return procList;
	}
	
	public void startProcess(ArrayList<String> procList) throws Exception {
		ProcLauncherEx procL = null;
		EventHandler evtHandler;
		for (int i = 0; i < procList.size(); i++) {
			procL = new ProcLauncherEx(procList.get(i), false);
			procL.setReadErrStream();
			evtHandler = new ExitHandler();
			procL.addEventHandler(evtHandler);
			procL.start();
			this.sleeping(5);
		}
	}
	
	public void startProcessEx(ArrayList<ProcInfoBean> procList) throws Exception {
		ProcLauncherEx procL = null;
		EventHandler evtHandler;
		for (int i = 0; i < procList.size(); i++) {
			ProcInfoBean bean = (ProcInfoBean) procList.get(i);
			procL = new ProcLauncherEx(bean.name, bean.cflag, false);
			procL.setReadErrStream();
			evtHandler = new ExitHandler();
			procL.addEventHandler(evtHandler);
			procL.start();
			this.sleeping(5);
		}
	}
	
	private void sleeping(int sec) {
		try {
			Thread.sleep(sec * 1000);
		} catch (InterruptedException ex) {
		}
	}
	
	public void run() {
		try {
			//ArrayList<String> procList = this.loadProcessList();
			//this.startProcess(procList);
			ArrayList<ProcInfoBean> procList = this.loadProcessListEx();
			this.startProcessEx(procList);
		} catch (Exception ex) {
			this.log.error(ex.toString(), ex);
		}

	}

}
